package com.app.micaihu.custom.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

/**
 * 解决滑动冲突的Gallery
 * @author linkai
 *
 */
public class PairGallery extends Gallery {

	private float mDownX;
	private float mDownY;
	private static final int DRAGGING_DIRECT_UNDEFINE = 0;
	private static final int DRAGGING_DIRECT_HORIZONTAL = 1;
	private static final int DRAGGING_DIRECT_VERTICAL = 2;
	private int mDraggingDirect = DRAGGING_DIRECT_UNDEFINE;

	public PairGallery(Context context){
		super(context);
	}

	public PairGallery(Context context, AttributeSet attrs) {
		super(context,attrs);
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mDownX = ev.getX();
			mDownY = ev.getY();
			mDraggingDirect = DRAGGING_DIRECT_UNDEFINE;
			if(mDownX < 50){
				getParent().requestDisallowInterceptTouchEvent(false);
			} else {
				getParent().requestDisallowInterceptTouchEvent(true);
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if ((mDraggingDirect == DRAGGING_DIRECT_UNDEFINE && Math.abs(ev.getX() - mDownX) > 30 )|| Math.abs(ev.getY() - mDownY)>10 ) {
				boolean isDraggingHorizontal = Math.abs(ev.getX() - mDownX) > Math.abs(ev.getY() - mDownY);
				getParent().requestDisallowInterceptTouchEvent(isDraggingHorizontal);
				mDraggingDirect = isDraggingHorizontal ? DRAGGING_DIRECT_HORIZONTAL : DRAGGING_DIRECT_VERTICAL;
			}
			break;
		case MotionEvent.ACTION_UP:
			break;
		case MotionEvent.ACTION_CANCEL:
			mDraggingDirect = DRAGGING_DIRECT_UNDEFINE;
			getParent().requestDisallowInterceptTouchEvent(false);
			break;
		default:
			break;
		}
		
		return super.dispatchTouchEvent(ev);
	}
	

}
